package com.yami.shop.service.impl;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.yami.shop.bean.model.UserCashOut;
import com.yami.shop.dao.UserCashOutMapper;
import com.yami.shop.service.UserCashOutService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 * 会员提现表 服务实现类
 * </p>
 *
 * @author jcd
 * @since 2024-07-26
 */
@Service
public class UserCashOutServiceImpl extends ServiceImpl<UserCashOutMapper, UserCashOut> implements UserCashOutService {

    /**
     * 计算个人所得税
     * @param uid
     * @param cashOutMoney
     * @return
     */
    @Override
    public Map<String, Object> calculateIndividualIncomeTax(String uid, String cashOutMoney) {
        BigDecimal bigDecimal1 = new BigDecimal(cashOutMoney);

        Date date = new Date();
        DateTime begin = DateUtil.beginOfMonth(date);
        DateTime end = DateUtil.endOfMonth(date);

        List<UserCashOut> list = this.lambdaQuery()
                .eq(UserCashOut::getUserAccountTable, "tz_user")
                .eq(UserCashOut::getUserAccountTableField, "distribution_bonus")
                .eq(UserCashOut::getUserAccountTableId, uid)
                .in(UserCashOut::getState, Arrays.asList(1, 3, 4,5))
                .between(UserCashOut::getCreateTime,begin,end)
                .list();

        //本月已提现金额
        BigDecimal reduce = list.stream().map(c -> c.getCashMoney()).reduce(BigDecimal.ZERO, BigDecimal::add);

        //阈值
        BigDecimal bigDecimal = new BigDecimal("5000");
        //个人所得税税率
        BigDecimal taxRate = new BigDecimal("0.05");

        Map<String,Object> rmap=new HashMap<>();

        if(reduce.compareTo(bigDecimal) < 0){
            //已提现金额不超过5000
            BigDecimal add = reduce.add(bigDecimal1);
            if(add.compareTo(bigDecimal) < 0){
                //本次提现后依然不超过5000
                rmap.put("individualIncomeTax",BigDecimal.ZERO);
                rmap.put("type","0");//总提现金额不超过5000,不收取个人所得税

                rmap.put("needToPayTaxes",BigDecimal.ZERO);//多少钱 * 费率 = 缴纳金额
                rmap.put("taxRate",taxRate);
            }else{
                //提现后超过5000
                //超出部分缴纳个人所得税
                BigDecimal subtract = add.subtract(bigDecimal);
                BigDecimal multiply = subtract.multiply(taxRate);

                rmap.put("individualIncomeTax",multiply);//缴纳金额
                rmap.put("type","1");//部分超出,超出部分需交税

                rmap.put("needToPayTaxes",subtract);//多少钱 * 费率 = 缴纳金额
                rmap.put("taxRate",taxRate);
            }
        }else{
            //已提现金额超过5000
            BigDecimal multiply = bigDecimal1.multiply(taxRate);

            rmap.put("individualIncomeTax",multiply);//缴纳金额
            rmap.put("type","2");//本次提现全部超出

            rmap.put("needToPayTaxes",bigDecimal1);//多少钱 * 费率 = 缴纳金额
            rmap.put("taxRate",taxRate);
        }
        return rmap;
    }
}
